<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/main.php
#	FUNCTION:		The 'hub' file for the site
#	AUTHOR:			Cameron Morrow
#	CREATED:		26/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# B U F F E R
ob_start();

# I N C L U D E S
include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/groups.php";
include "../includes/pages.php";
include "../includes/xmls.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/files.php";
include "../includes/text.php";
include "../includes/tableproperties.php";
include "../includes/shopping_cart.php";
include "../includes/init.php";

# C H E C K   F O R   C U R R E N T   L O G   I N
$IS_AUTHORIZED = $USER -> isUserAuthorized($PAGES -> getAuthorizedGroups($PAGE_ID));

//print_r($USER);


# I S   A   P A G E   P R E V I E W
$PAGE_PREVIEW = ($PAGE_ID == "pagepreview" && $USER -> getCanApprove());

# F O R C I N G   L O G I N
$FORCE_LOGIN = (@$_GET["forcelogin"] && !$USER -> isLoggedIn()) ? true : false;

# Is showing a log in?
$IS_SHOWING_LOGIN = false;

//print_r($_SESSION[$PROJECT_CODE . "group"]);

# G E T   C O N T E N T
if ($DATABASE_CONNECTION_ESTABLISHED) {
	if ($IS_AUTHORIZED && !$FORCE_LOGIN) {

		# If using custom content...
		if ($PAGE -> getData("p_custom_content") != "") {

			# Check to see if page is in the site specific folder
			if (file_exists("../site_specific/" . $PAGE -> getData("p_custom_content") . ".php")) {

				# Include
				include("../site_specific/" . $PAGE -> getData("p_custom_content") . ".php");

			} 
			
			else if(file_exists("../includes/ajax/" . $PAGE -> getData("p_custom_content") . ".php")){

				# Include
				include("../includes/ajax/" . $PAGE -> getData("p_custom_content") . ".php");
			}
			else if(file_exists("../includes/json/" . $PAGE -> getData("p_custom_content") . ".php")){

				# Include
				include("../includes/json/" . $PAGE -> getData("p_custom_content") . ".php");
			}
			else{

				# Check to see if page is in the custom folder
				if (file_exists("../includes/custom/" . $PAGE -> getData("p_custom_content") . ".php")) {

					# Include
					include("../includes/custom/" . $PAGE -> getData("p_custom_content") . ".php");

				} else {

					$PAGE_CONTENT = "<h2>Custom content not found</h2><p>Sorry, the content could not be displayed, because the custom handler for this page (<em>" . $PAGE -> getData("p_custom_content") . "</em>) was not found.</p>";
				}
			}

		} else {

			$PAGE_CONTENT = $PAGE -> getData("p_content");
		}
	} else {
		
		if($PAGE -> getData('p_section') != "ajax"){

			# If previewing
			if ($PAGE_PREVIEW) {
	
				# Preview Page
				$preview_page = (@$_GET["page"]) ? removeInvalidChars($_GET["page"], $VALID_CHAR_LIST["ids"]) : "";
	
				# Get data of page
				$sql = "SELECT p_content, p_custom_content FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_id = '" . $preview_page . "'";
	
				$data = getRecord($sql);
	
				if (count($data) > 0) {
	
					$PAGE_CONTENT = $data[0]["p_content"];
	
					if ($data[0]["p_custom_content"] != "") {
						addMessage("<p><strong>Note when previewing this page</strong>: This page uses a custom handler, so the content you see here may not appear in this form in the actual page.</p>", 2);
					}
	
				} else {
	
					$PAGE_CONTENT = "<p>Page not found.</p>";
				}
	
			} else {
				
				
	
				# If not authorized, but logged in, redirect
				if ($USER -> isLoggedIn()) {
	
					$PAGE_NAME = "Not Authorized";
					$PAGE_CONTENT = $USER -> generateUnauthorizedMessage();
	
				} else {
					
					if($PAGE -> getData("p_section") != 'ajax'){
	
						$PAGE_NAME = "Log In";
						$PAGE_CONTENT = $USER -> generateLoginForm($AUTHORIZATION_MESSAGE);
						$IS_SHOWING_LOGIN = true;
					}
					else{
						
						header( 'Location:http://www.zone4.com.au/dev/secondrio/site/xmlsiteeditor.php' ) ;
					}
				}
			}
		}
		else{
			
			if(file_exists("../includes/ajax/" . $PAGE -> getData("p_custom_content") . ".php")){

				# Include
				include("../includes/ajax/" . $PAGE -> getData("p_custom_content") . ".php");
			}
		}
	}
} else {

	$PAGE_CONTENT = "<h2>A critical problem has occured</h2><p>Sorry, a critical error has occured and the site database cannot be accessed. Please try accessing the site later on.</p>";
}

# E N S U R E   S O M E   C O N T E N T

//echo "PAGE_CONTENT:".$PAGE_CONTENT."<br/>";
if ($PAGE_CONTENT == "") {

	if($PAGE -> getData("p_section") != "json"){
		$PAGE_CONTENT = "<h2>Sorry, no content yet</h2><p>No content has been created for this page yet.</p>";
	}
}

# D O   O N - A C C E S S   A C T I O N S
if ($PAGE -> getData("p_accessaction") != "") {
	eval($PAGE -> getData("p_accessaction"));
}

# O U T P U T
if ($PAGE -> getData("p_section") == "site" || $PAGE_ID == "pagepreview") {
	include("../site_specific/" . $PROJECT_CODE . ".php");
} 
else if($PAGE -> getData("p_section") == "ajax"){
	
	include("../includes/ajax/ajax.php");
}
else if($PAGE -> getData("p_section") == "json"){

	include("../includes/json/json.php");
}
else {
	include("../includes/format/admin.php");
}

ob_end_flush();

?>